package com.xyoto.community.service.impl;

import com.xyoto.community.constant.DeleteStatus;
import com.xyoto.community.constant.moduleConstant;
import com.xyoto.community.dto.SaveGuestBookDTO;
import com.xyoto.community.cache.UserCache;
import com.xyoto.community.entity.main.GuestBook;
import com.xyoto.community.enums.GuestBookEnum;
import com.xyoto.community.exception.ExceptionUtils;
import com.xyoto.community.mapper.main.GuestBookMapper;
import com.xyoto.community.service.GuestBookService;
import com.xyoto.community.service.UserCacheService;
import com.xyoto.community.utils.JwtUtils;
import com.xyoto.community.utils.RedisCommonUtils;
import com.xyoto.community.utils.RedisUtils;
import com.xyoto.community.utils.Result;
import com.xyoto.community.vo.GuestBookVO;
import io.jsonwebtoken.JwtBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 留言板实现类
 */
@Service
@Slf4j
public class GuestBookServiceImpl implements GuestBookService {

    @Autowired
    private GuestBookMapper guestBookMapper;

    @Resource
    private HttpServletRequest request;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private GuestBookRedisImpl guestBookRedis;

    @Autowired
    private RedisCommonUtils redisCommonUtils;

    @Autowired
    private UserCacheService userCacheService;

    /**
     * 新增留言
     * @param saveGuestBookDTO
     * @return
     */
    @Override
    public Result saveGuestBook(SaveGuestBookDTO saveGuestBookDTO) {

        log.info("开始执行新增留言功能");

        Integer userId = JwtUtils.getMemberIdByJwtToken(request);

        UserCache userCache = userCacheService.getUserMessageFromUserCacheById(userId);

        //将userPo 和 saveGuestBookDTO 数据 放入guestBook
        GuestBook guestBook = new GuestBook();
        guestBook.setUserId(userCache.getId());
        guestBook.setContent(saveGuestBookDTO.getContent());
        guestBook.setDeleteStatus(DeleteStatus.NO);

        log.info("开始执行新增留言SQL");
        Integer saveGuestBook = guestBookMapper.saveGuestBook(guestBook);
        if (saveGuestBook == null || saveGuestBook == 0){
            ExceptionUtils.error(GuestBookEnum.GUEST_BOOK_CREAT_FAIL);
        }

        log.info("开始将留言存入redis");
        GuestBookVO guestBookVO = new GuestBookVO();
        guestBookVO.setContent(guestBook.getContent());
        guestBookVO.setUserId(guestBook.getUserId());
        guestBookVO.setId(guestBook.getId());
        guestBookVO.setNickname(userCache.getNickname());
        guestBookVO.setAvatarUrl(userCache.getAvatarUrl());
        guestBookRedis.saveGuestBook(guestBookVO);


        return Result.success();
    }

    /**
     * 获取前两白条留言数据
     * @return
     */
    @Override
    public Result getGuestBookList() {

        //从redis中取出留言数据
        List<Object> guestBookList = redisCommonUtils.lGet(moduleConstant.GUEST_BOOK,0,-1);
        //若留言数据小于200条，则从数据库中获取相应的数据
        if (guestBookList.size() < 200){
            Integer count = guestBookMapper.getGuestBookCount();
            int length;
            if (count < 200){
                length = count - guestBookList.size();
            } else {
                length = 200 - guestBookList.size();
            }
            List<GuestBookVO> guestBooks = guestBookMapper.getGuestBookList(length);
            for (GuestBookVO guestBook : guestBooks) {
                guestBookRedis.saveGuestBook(guestBook);
            }
            List<Object> newGuestBookList = redisCommonUtils.lGet(moduleConstant.GUEST_BOOK,0,-1);
            return Result.success(newGuestBookList);
        }
        return Result.success(guestBookList);
    }
}
